package com.py.framework.controller;

import com.py.framework.common.Constants;
import com.py.framework.common.controller.SystemController;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.helper.HttpServletHelper;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.core.utils.BeanUtil;
import com.py.framework.dao.IntegrationMenuResourceDao;
import com.py.framework.entity.MenuEntity;
import com.py.framework.entity.MenuTree;
import com.py.framework.entity.RoleEntity;
import com.py.framework.entity.UserEntity;
import com.py.framework.enums.MenuType;
import com.py.framework.service.AuthenticationService;
import com.py.framework.service.IntegrationMenuResourceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 政务一体化网站菜单资源管理
 */
@RestController
@RequestMapping("/integrationMenuresource")
public class CustomizationIntegrationController extends SystemController {

    /**
     * 未读取到配置文件中的顶级资源名称，使用默认值
     */
    private static final String topName = "共享平台";

    private String integrationMenuName = "共享平台";
    
    @Autowired
    private AuthenticationService authenticationService;
    
    
    @Autowired
    private IntegrationMenuResourceDao integrationMenuResourceDao;

    private static Map<String, Object> topResource = new HashMap<>();

    {
        topResource.put("id", 0);
        topResource.put("parentId", 0);
        topResource.put("allParentId", null);
        topResource.put("menuType", 1);
        topResource.put("linkUrl", null);
        topResource.put("routeAddr", null);
        topResource.put("orderBy", 1);
        topResource.put("isWinOpen", 2);
        topResource.put("parentMenuName", "");
        topResource.put("menuName", StringUtils.isNotBlank(integrationMenuName) ? integrationMenuName : topName);
    }

    @Autowired
    private IntegrationMenuResourceService integrationMenuResourceService;

    /**
     * @api {post} /menuresource/addMenuResourceInfo.json 添加菜单资源实体
     * @apiName addMenuResourceInfo
     * @apiVersion 0.0.1
     * @apiGroup 菜单资源管理
     * @apiParam {MenuEntity} entity 菜单资源实体
     * @apiParam {String} menuName 菜单名称
     * @apiParam {long} parentId 父节点ID
     * @apiParam {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiParam {String} linkUrl 后台路由
     * @apiParam {String} routeAddr 前台路由地址
     * @apiParam {String} icon ICON图标
     * @apiParam {int} orderBy 排序
     * @apiParam {int} isShow 是否显示，1：是，2：否
     * @apiParam {int} isWinOpen 是否新窗口打开，1：是，2：否
     * @apiParamExample {json} 请求参数示例
     * {"menuName":"资源管理","parentId":1,"menuType":1,"linkUrl":"http://xxx.com","routeAddr":"menu_sys","orderBy":1,"isShow":1,"isWinOpen":1,"icon":"header.ico"}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":"","count":0,"data": ""}
     */
    @RequestMapping(value = "/addMenuResourceInfo", method = RequestMethod.POST)
    public RestServiceResult<Object> addMenuResourceInfo(@RequestBody MenuEntity entity) {
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        if (null != entity) {
            try {
                integrationMenuResourceService.addMenuResourceInfo(entity);
            } catch (Exception e) {
                result = super.getFailedRestServiceResult("添加失败！");
                ExceptionLogger.error(e);
            }
        }
        return result;
    }
    
    @RequestMapping(value = "/isResourceInfo", method = RequestMethod.POST)
    public RestServiceResult<Object> isResourceInfo(HttpServletRequest request,String url) {
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        String jdToken = HttpServletHelper.getUserToken(request);
        String data = "N";
        try {
	        UserEntity user = authenticationService.getCurrentUserInfo(jdToken);
	        if (null != user && user.getUserType() == 3) {
	        	data = "Y";
	        }else{
	        	Long userId = user.getId();
		        Long roleId = integrationMenuResourceDao.selectUserRoleId(userId);
		        int i = integrationMenuResourceService.isResourceInfo(roleId,url);
		        if(i>0){
		        	data = "Y";
		        }
	        }
	        
        } catch (Exception e) {
        	 result.setData(data);
             return result;
        }
        result.setData(data);
        return result;
    }

    /**
     * @api {post} /menuresource/queryMenuResourceForPage.json 分页插叙菜单资源列表信息
     * @apiName queryMenuResourceForPage
     * @apiVersion 0.0.1
     * @apiGroup 菜单资源管理
     * @apiParam {int} page  当前页码
     * @apiParam {int} limit 每页条数
     * @apiParam {String} menuName 菜单名称
     * @apiParam {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiParam {long}  parentId 父ID
     * @apiParamExample {json} 请求参数示例
     * {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} menuName 菜单名称
     * @apiSuccess {long} parentId 父节点ID
     * @apiSuccess {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiSuccess {String} linkUrl 后台路由
     * @apiSuccess {String} routeAddr 前台路由地址
     * @apiSuccess {String} icon ICON图标
     * @apiSuccess {int} orderBy 排序
     * @apiSuccess {int} isShow 是否显示，1：是，2：否
     * @apiSuccess {int} isWinOpen 是否新窗口打开，1：是，2：否
     * @apiSuccess {String} parentMenuName 父节点名称
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":"","count":3,"data": [{"menuName":"资源管理","parentId":1,"menuType":1,"linkUrl":"http://xxx.com","routeAddr":"menu_sys","orderBy":1,"isShow":1,"isWinOpen":1,"icon":"header.ico"}]}
     */
    @RequestMapping(value = "/queryMenuResourceForPage", method = RequestMethod.POST)
    public RestServiceResult<List<MenuEntity>> queryMenuResourceForPage(int page, int limit, String menuName,
                                                                        @RequestParam(value = "menuType", required = false, defaultValue = "0") int menuType,
                                                                        @RequestParam(value = "parentId", required = false, defaultValue = "0") long parentId) {
        Map<String, Object> params = new HashMap<>();
        Page pager = super.getPage(page, limit);
        params.put("page", pager);
        params.put("menuName", menuName);
        params.put("menuType", menuType);
        params.put("parentId", parentId);
        List<MenuEntity> datas = integrationMenuResourceService.queryMenuResourceForPage(params);
        return super.getPageSuccessRestServiceResult(pager.getTotalCount(), datas);
    }

    /**
     * @api {post} /menuresource/getAllMenuResources.json 查询所有菜单资源数据
     * @apiName getAllMenuResources
     * @apiVersion 0.0.1
     * @apiGroup 菜单资源管理
     * @apiParamExample {json} 请求参数示例
     * {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} menuName 菜单名称
     * @apiSuccess {long} parentId 父节点ID
     * @apiSuccess {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiSuccess {String} linkUrl 后台路由
     * @apiSuccess {String} routeAddr 前台路由地址
     * @apiSuccess {String} icon ICON图标
     * @apiSuccess {int} orderBy 排序
     * @apiSuccess {int} isShow 是否显示，1：是，2：否
     * @apiSuccess {int} isWinOpen 是否新窗口打开，1：是，2：否
     * @apiSuccess {String} parentMenuName 父节点名称
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":"","count":3,"data": [{"menuName":"资源管理","parentId":1,"menuType":1,"linkUrl":"http://xxx.com","routeAddr":"menu_sys","orderBy":1,"isShow":1,"isWinOpen":1,"icon":"header.ico"}]}
     */
    @RequestMapping(value = "/getAllMenuResources", method = RequestMethod.POST)
    public RestServiceResult<List<Map<String, Object>>> getAllMenuResources() {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        String name = StringUtils.isNotBlank(integrationMenuName) ? integrationMenuName : topName;
        topResource.put("menuName", name);
        result.add(topResource);
        Map<String, Object> item = null;
        List<MenuEntity> datas = integrationMenuResourceService.getMenuResourceByParentId(null);
        if (!CollectionUtils.isEmpty(datas)) {
            for (MenuEntity menuEntity : datas) {
                try {
                    item = BeanUtil.populateBean2Map(menuEntity);
                    if (null != item) {
                        // if (null != menuEntity && 1 == menuEntity.getMenuType()) {
                        // item.put("parentMenuName", name);
                        // }
                        item.remove("icon");
                        result.add(item);
                    }
                } catch (Exception e) {
                    ExceptionLogger.error(e);
                }
            }
        }
        return super.getSuccessRestServiceResult(result);
    }

    /**
     * @api {post} /menuresource/getMenuResourceByParentId.json 根据父Id查询所有子节点数据
     * @apiName getMenuResourceByParentId
     * @apiVersion 0.0.1
     * @apiGroup 菜单资源管理
     * @apiParam {String}  parentId 父Id
     * @apiParamExample {json} 请求参数示例
     * {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} menuName 菜单名称
     * @apiSuccess {long} parentId 父节点ID
     * @apiSuccess {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiSuccess {String} linkUrl 后台路由
     * @apiSuccess {String} routeAddr 前台路由地址
     * @apiSuccess {String} icon ICON图标
     * @apiSuccess {int} orderBy 排序
     * @apiSuccess {int} isShow 是否显示，1：是，2：否
     * @apiSuccess {int} isWinOpen 是否新窗口打开，1：是，2：否
     * @apiSuccess {String} parentMenuName 父节点名称
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":"","count":3,"data": [{"menuName":"资源管理","parentId":1,"menuType":1,"linkUrl":"http://xxx.com","routeAddr":"menu_sys","orderBy":1,"isShow":1,"isWinOpen":1,"icon":"header.ico"}]}
     */
    @RequestMapping(value = "/getMenuResourceByParentId", method = RequestMethod.POST)
    public RestServiceResult<List<MenuEntity>> getMenuResourceByParentId(String parentId) {
        List<MenuEntity> datas = null;
        if (StringUtils.isNotBlank(parentId)) {
            datas = integrationMenuResourceService.getMenuResourceByParentId(parentId);
        }
        return super.getSuccessRestServiceResult(datas);
    }

    /**
     * @api {post} /menuresource/getSysMenuResource.json 获取系统菜单数据
     * @apiName getSysMenuResource
     * @apiVersion 0.0.1
     * @apiGroup 菜单资源管理
     * @apiParamExample {json} 请求参数示例
     * {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} menuName 菜单名称
     * @apiSuccess {long} parentId 父节点ID
     * @apiSuccess {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiSuccess {String} linkUrl 后台路由
     * @apiSuccess {String} routeAddr 前台路由地址
     * @apiSuccess {String} icon ICON图标
     * @apiSuccess {int} orderBy 排序
     * @apiSuccess {int} isShow 是否显示，1：是，2：否
     * @apiSuccess {int} isWinOpen 是否新窗口打开，1：是，2：否
     * @apiSuccess {String} parentMenuName 父节点名称
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":"","count":3,"data": [{"menuName":"资源管理","parentId":1,"menuType":1,"linkUrl":"http://xxx.com","routeAddr":"menu_sys","orderBy":1,"isShow":1,"isWinOpen":1,"icon":"header.ico"}]}
     */
    @RequestMapping(value = "/getSysMenuResource", method = RequestMethod.POST)
    public RestServiceResult<List<MenuEntity>> getSysMenuResource() {
        // 根据菜单类型查询菜单资源数据, 1：子系统
        List<MenuEntity> datas = integrationMenuResourceService.getMenuResourceByMenuType(MenuType.TYPE_1.getValue());
        return super.getSuccessRestServiceResult(datas);
    }

    /**
     * @api {post} /menuresource/findOneMenuResource.json 根据Id查询菜单资源实体
     * @apiName findOneMenuResource
     * @apiVersion 0.0.1
     * @apiGroup 菜单资源管理
     * @apiParam {long}  id 菜单资源Id
     * @apiParamExample {json} 请求参数示例
     * {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} menuName 菜单名称
     * @apiSuccess {long} parentId 父节点ID
     * @apiSuccess {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiSuccess {String} linkUrl 后台路由
     * @apiSuccess {String} routeAddr 前台路由地址
     * @apiSuccess {String} icon ICON图标
     * @apiSuccess {int} orderBy 排序
     * @apiSuccess {int} isShow 是否显示，1：是，2：否
     * @apiSuccess {int} isWinOpen 是否新窗口打开，1：是，2：否
     * @apiSuccess {String} parentMenuName 父节点名称
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":"","count":0,"data": {"menuName":"资源管理","parentId":1,"menuType":1,"linkUrl":"http://xxx.com","routeAddr":"menu_sys","orderBy":1,"isShow":1,"isWinOpen":1,"icon":"header.ico"}}
     */
    @RequestMapping(value = "/findOneMenuResource", method = RequestMethod.POST)
    public RestServiceResult<MenuEntity> findOneMenuResource(long id) {
        MenuEntity data = null;
        if (id > Constants.NUMBER_ZERO) {
            data = integrationMenuResourceService.findOneMenuResource(id);
        }
        if (null != data && 1 == data.getMenuType()) {
            String name = StringUtils.isNotBlank(integrationMenuName) ? integrationMenuName : topName;
            data.setParentMenuName(name);
        }
        return super.getSuccessRestServiceResult(data);
    }

    /**
     * @api {post} /menuresource/updateMenuResource.json 更新菜单资源信息
     * @apiName updateMenuResource
     * @apiVersion 0.0.1
     * @apiGroup 菜单资源管理
     * @apiParam {MenuEntity}  entity 菜单资源
     * @apiParam {String} menuName 菜单名称
     * @apiParam {long} parentId 父节点ID
     * @apiParam {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiParam {String} linkUrl 后台路由
     * @apiParam {String} routeAddr 前台路由地址
     * @apiParam {String} icon ICON图标
     * @apiParam {int} orderBy 排序
     * @apiParam {int} isShow 是否显示，1：是，2：否
     * @apiParam {int} isWinOpen 是否新窗口打开，1：是，2：否
     * @apiParamExample {json} 请求参数示例
     * {"menuName":"资源管理","parentId":1,"menuType":1,"linkUrl":"http://xxx.com","routeAddr":"menu_sys","orderBy":1,"isShow":1,"isWinOpen":1,"icon":"header.ico"}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {String} result
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":"","count":0,"data": ""}
     */
    @RequestMapping(value = "/updateMenuResource", method = RequestMethod.POST)
    public RestServiceResult<Object> updateMenuResource(@RequestBody MenuEntity entity) {
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        if (null != entity) {
            try {
                integrationMenuResourceService.updateMenuResource(entity);
            } catch (Exception e) {
                result = super.getFailedRestServiceResult("更新失败！");
                ExceptionLogger.error(e);
            }
        }
        return result;
    }

    /**
     * @api {post} /menuresource/batchDelMenuResource.json 批量删除菜单资源
     * @apiName batchDelMenuResource
     * @apiVersion 0.0.1
     * @apiGroup 菜单资源管理
     * @apiParam {String}  ids 菜单资源Id集合
     * @apiParamExample {json} 请求参数示例
     * {}
     * @apiSuccess {String} code 状态码:1 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":"","count":0,"data": ""}
     */
    @RequestMapping(value = "/batchDelMenuResource", method = RequestMethod.POST)
    public RestServiceResult<Object> batchDelMenuResource(String ids) {
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        if (StringUtils.isNotBlank(ids)) {
            try {
                integrationMenuResourceService.batchDelMenuResource(super.convert2Long(ids));
            } catch (ServiceException e) {
                result = super.getFailedRestServiceResult(e.getMessage());
                ExceptionLogger.error(e);
            } catch (Exception e) {
                result = super.getFailedRestServiceResult("删除失败！");
                ExceptionLogger.error(e);
            }
        }
        return result;
    }

    /**
     * @api {post} /integrationMenuresource/getIntegrationMenus.json 获取当前用户政务一体化菜单数据
     * @apiName getIntegrationMenus
     * @apiVersion 0.0.1
     * @apiGroup 对外接口
     * @apiParam {HttpServletRequest} request 请求实例
     * @apiParamExample {json} 请求参数示例 {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {long} id 菜单Id
     * @apiSuccess {long} parentId 父菜单Id
     * @apiSuccess {String} name 菜单地址
     * @apiSuccess {String} title 菜单名称
     * @apiSuccess {int} menuType 菜单类型，1：子系统，2：模块，3：功能，4：资源
     * @apiSuccess {String} icon ICON图标
     * @apiSuccess {int} orderBy 排序
     * @apiSuccess {int} isShow 是否显示，1：是，2：否
     * @apiSuccess {int} isWinOpen 是否新窗口打开，1：是，2：否
     * @apiSuccessExample {json} 成功返回结果 {"code":0,"msg":"","count":1,"data":
     * [{"id":10,"title":"资源管理","parentId":1,"menuType":1,"name":"menu_sys","orderBy":1,"isShow":1,"isWinOpen":1,"icon":"header.ico"}]}
     */
    @RequestMapping(value = "/getIntegrationMenus.json", method = RequestMethod.POST)
    public RestServiceResult<List<MenuTree>> getIntegrationMenus(HttpServletRequest request) {
        List<MenuEntity> result = new ArrayList<>();
        String jdToken = HttpServletHelper.getUserToken(request);
        if (StringUtils.isNotBlank(jdToken)) {
            try {
                List<MenuEntity> datas = integrationMenuResourceService.getCurrentUserMenuResources(jdToken);
                if (!CollectionUtils.isEmpty(datas)) {
                    for (MenuEntity menu : datas) {
                        if (null != menu && (menu.getMenuType() == MenuType.TYPE_1.getValue()
                                || menu.getMenuType() == MenuType.TYPE_2.getValue()
                                || menu.getMenuType() == MenuType.TYPE_3.getValue()
                                || menu.getMenuType() == MenuType.TYPE_5.getValue())) {
                            result.add(menu);
                        }
                    }
                }
            } catch (Exception e) {
                ExceptionLogger.error(e);
            }
        }
        // 组装用户菜单树
        List<MenuTree> datas = this.populateMenuTrees(result);
        return super.getSuccessRestServiceResult(datas);
    }

    /**
     * @api {post} /role/getRoleMenuRelate.json 根据角色Id获取角色资源关联数据
     * @apiName getRoleMenuRelate
     * @apiVersion 0.0.1
     * @apiGroup 角色管理
     * @apiParam {long} roleId 角色Id
     * @apiParamExample {json} 请求参数示例
     *  {}
     * @apiSuccess {String} code 状态码:0 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccess {long} id 角色Id
     * @apiSuccess {String} roleName 角色名称
     * @apiSuccess {long} menuId 菜单Id
     * @apiSuccessExample {json} 成功返回结果
     *   {"code":0,"msg":null,"count":1,"data":[{"id":1,"roleName":"超级管理员","menuId":2}]}
     */
    @RequestMapping(value = "/getRoleMenuRelate", method = RequestMethod.POST)
    public RestServiceResult<List<RoleEntity>> getRoleMenuRelate(long roleId) {
        List<RoleEntity> datas = null;
        if (roleId > Constants.NUMBER_ZERO) {
            datas = integrationMenuResourceService.getIntegrationMenuRelate(roleId);
        }
        return super.getSuccessRestServiceResult(datas);
    }

    /**
     * 修改政务一体化网站菜单资源
     * @param datas
     * @return
     */
    @RequestMapping(value = "/modifyRegionMenuRelate", method = RequestMethod.POST)
    public RestServiceResult<Object> modifyRegionMenuRelate(@RequestBody List<RoleEntity> datas) {
        RestServiceResult<Object> result = super.getSuccessRestServiceResult();
        if (! CollectionUtils.isEmpty(datas)) {
            try {
                integrationMenuResourceService.modifyRegionMenuRelate(datas);
            } catch (Exception e) {
                result = super.getFailedRestServiceResult("修改失败！");
                ExceptionLogger.error(e);
            }
        }
        return result;
    }

    /**
     * 组装用户菜单树
     *
     * @param menus
     * @return
     */
    private List<MenuTree> populateMenuTrees(List<MenuEntity> menus) {
        List<MenuTree> trees = new ArrayList<>();
        MenuTree treeNode = null;
        if (!CollectionUtils.isEmpty(menus)) {
            for (MenuEntity menu : menus) {
                // 先找到所有的一级菜单
                if (null != menu && Constants.NUMBER_ZERO == menu.getParentId()) {
                    treeNode = this.toMenuTree(menu);
                    trees.add(treeNode);
                }
            }
            // 为一级菜单设置子菜单，getChild是递归调用的
            for (MenuTree menu : trees) {
                menu.setList(getChild(menu.getId(), menus));
            }
        }
        return trees;
    }

    /**
     * 递归查找子菜单
     *
     * @param id    当前菜单id
     * @param menus 要查找的列表
     * @return
     */
    private List<MenuTree> getChild(long id, List<MenuEntity> menus) {
        // 子菜单
        List<MenuTree> childList = new ArrayList<>();
        MenuTree treeNode = null;
        for (MenuEntity menu : menus) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (menu.getParentId() > Constants.NUMBER_ZERO) {
                if (menu.getParentId() == id) {
                    treeNode = this.toMenuTree(menu);
                    childList.add(treeNode);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (MenuTree menu : childList) {
            // 菜单类型不是资源的子菜单还有子菜单
            if (menu.getMenuType() != MenuType.TYPE_4.getValue()) {
                // 递归
                menu.setList(getChild(menu.getId(), menus));
            }
        }
        // 递归退出条件
        if (childList.size() == Constants.NUMBER_ZERO) {
            return null;
        }
        return childList;
    }

    /**
     * 转换菜单实体
     *
     * @param menu
     * @return
     */
    private MenuTree toMenuTree(MenuEntity menu) {
        MenuTree tree = new MenuTree();
        if (null != menu) {
            tree.setId(menu.getId());
            tree.setParentId(menu.getParentId());
            tree.setName(menu.getRouteAddr());
            tree.setTitle(menu.getMenuName());
            tree.setIcon(menu.getIcon());
            tree.setOrderBy(menu.getOrderBy());
            tree.setMenuType(menu.getMenuType());
            tree.setIsShow(menu.getIsShow());
            tree.setIsWinOpen(menu.getIsWinOpen());
        }
        return tree;
    }
    
}
